<template>

  <el-form ref="regForm" :model="regForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
    <div class="title-container">
      <h3 class="title">数字孪生PAAS平台后台管理</h3>
      <span class="title-small">Digital twin PAAS platform</span>
    </div>

    <el-card class="box-card" style="width:23.125rem;margin-left: 2.5rem;">
      <div slot="header" class="clearfix" >
        <span style="color: rgb(51, 51, 51);
            font-weight: bold;
            margin-bottom: 10px;
            display: block;
        ">用户注册</span>
        <div style="width: 100%;height: 1px;background: #d7d7d7;"></div>
      </div>
      <div style="margin-top: 0px;">

        <el-form-item prop="phone">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="phone"
            v-model="regForm.phone"
            placeholder="请输入手机号"
            name="phone"
            type="text"
            tabindex="1"
            autocomplete="off"
          />
          <div  style="
              position: inherit;
              float: right;
              margin-right: 8px;
              margin-top: -42px;
          ">
            <el-button size="mini" class="el-code-me" @click="getCode" :loading="loading">验证码</el-button>
          </div>
        </el-form-item>

        <el-form-item prop="code">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="code"
            v-model="regForm.code"
            placeholder="请输入验证码"
            name="code"
            type="text"
            tabindex="1"
            autocomplete="off"
          />
        </el-form-item>

        <el-tooltip v-model="capsTooltip" content="大写输入已打开" placement="right" manual>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="regForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              autocomplete="off"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>


        <el-form-item prop="name">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="name"
            v-model="regForm.name"
            placeholder="请输入真实姓名"
            name="name"
            type="text"
            tabindex="1"
            autocomplete="off"
          />
        </el-form-item>


        <el-form-item prop="orgName">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="orgName"
            v-model="regForm.orgName"
            placeholder="请输入所属单位"
            name="orgName"
            type="text"
            tabindex="1"
            autocomplete="off"
          />
        </el-form-item>

      </div>
      <el-button :loading="loading" type="primary custom-button" style="width:100%;margin-bottom:30px;" @click="submitForm">注册</el-button>
      <el-row :gutter="20" style="margin-top: -16px;">
        <el-col :span="16">
          <div style="text-align: left;">
            <a class="el-link-me" @click="handleBackLogin">返回登录</a>
          </div>
        </el-col>
        <el-col :span="8">
          <div style="text-align: right;">
          </div>
        </el-col>
      </el-row>
    </el-card>
  </el-form>

</template>

<script>
import { isBlank } from '@/utils/validate'
  import {
    getCode,
    regUser,
  } from '@/api/system/login'

import { JSEncrypt } from 'jsencrypt'

export default {
  name: 'RegForm',
  components: {  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isBlank(value)) {
        callback(new Error('请填写真实姓名！'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码至少6位！'))
      } else {
        callback()
      }
    }

    	let reg=/^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[3-8]{1})|(18[0-9]{1})|(19[0-9]{1})|(14[5-7]{1}))+\d{8})$/
    	//验证方法
    	let validatorPhone =  (phone, value, callback) =>  {
    		if (value === '') {
    			callback(new Error('手机号不能为空'))
    	  } else if (!reg.test(value)) {
    			callback(new Error('手机号格式错误'))
    		} else {
    			callback()
    		}
      }

    return {
      regForm: {
        phone: '',
        name: '',
        orgName: '',
        password: '',
        code:''
      },
      loginRules: {
        code: [{
          required: true,
          message: '请输入验证码',
          trigger: 'blur'
        },
        {
          min: 6,
          max: 6,
          message: '长度在 6 个字符',
          trigger: 'blur'
        }
        ],
        name: [{
          required: true,
          message: '请输入真实姓名',
          trigger: 'blur'
        },
        {
          min: 2,
          max: 50,
          message: '长度在 2 到 50 个字符',
          trigger: 'blur'
        }
        ],
        orgName: [{
          required: true,
          message: '请输入所属单位名称',
          trigger: 'blur'
        },
        {
          min: 2,
          max: 50,
          message: '长度在 2 到 50 个字符',
          trigger: 'blur'
        }
        ],
        phone: [{ required: true, trigger: 'blur', validator: validatorPhone }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      publicKey:'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKyiOmdLfcrpepp8sbbFFDJ44NMEJUIAG9vTcD9S9YY9+H8INT804Y4W/egfdBdpm2AMbCs+rhWkETLjNqKJYdcCAwEAAQ==',
    }
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    submitForm: function() {
      this.$refs.regForm.validate((valid) => {
        if (valid) {
          this.loading = true
          regUser({
            phone: this.encryptedData(this.regForm.phone),
            name: this.encryptedData(this.regForm.name),
            orgName: this.encryptedData(this.regForm.orgName),
            password: this.encryptedData(this.regForm.password),
            code:this.encryptedData(this.regForm.code)
          }).then(rsp => {
            this.$message({
              type: 'success',
              message:rsp.message
            })
             this.$emit('change', 'Login')
          }).catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.loading = false
          })
        }
      })
    },
    //  加密
    encryptedData(data) {
      // 新建JSEncrypt对象
      let encryptor = new JSEncrypt();
      // 设置公钥
      encryptor.setPublicKey(this.publicKey);
      // 加密数据
      return encryptor.encrypt(data);
    },
    getCode(){
      let reg=/^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[3-8]{1})|(18[0-9]{1})|(19[0-9]{1})|(14[5-7]{1}))+\d{8})$/
      //验证方法
      let validatorPhone =  (phone, value, callback) =>  {
      	if (value === '') {
      		callback(new Error('手机号不能为空'))
        } else if (!reg.test(value)) {
      		callback(new Error('手机号格式错误'))
      	} else {
      		callback()
      	}
      }
      if(this.regForm.phone===''){
        this.$message({
          type: 'error',
          message: '手机号不能为空.'
        })
        return;
      }
      else if (!reg.test(this.regForm.phone)) {
        this.$message({
          type: 'error',
          message: '手机号格式错误.'
        })
        return;
      }
      getCode(this.regForm.phone).then(response => {
        if(200==response.code){
          this.$message({
            type: 'success',
            message: response.message
          })
          this.regForm.code=response.result;
        }
      }).catch(err => {
        console.log(err);
      })
      .finally(() => {
      })
    },
    handleBackLogin(){
       this.$emit('change', 'Login')
    }
  }
}
</script>


<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#61a4aa;
$light_gray:#7f7f7f;
$cursor: #7f7f7f;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }


  .el-card__header {
    border: none;
  }

  .el-card__body {
      padding-top: 0px;
  }


  .el-link-me {
      color: #169bd5;
      text-decoration:none;
      font-size: 14px;
  }

  .el-link-me:hover {
      color: #4b4b4b;
      text-decoration:none;
  }

  .el-code-me {
      background-color: #169bd5;
      font-size: 14px;
      color: white;
  }

  .el-code-me:hover {
    background-color: #117caa;
  }

/* reset element-ui css */
.login-container {

  .el-input {
    display: inline-block;
    height: 35px;
    width: 85%;

    input {
     /* background: transparent; */
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 30px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }



  .el-form-item {
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color:  $light_gray;
    background-color: white;
    border: 1px solid $light_gray;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#7f7f7f;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
 background-color: #aaaaaa;
  overflow: hidden;


  .login-card {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }


  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 30px;
      color:#333333;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .title-small{
      margin-top: -30px;
      display: block;
      margin-bottom: 35px;
      text-align: center;
      font-size: 25px;
      font-weight: bold;
      color: #333333;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
